<template>
	<el-main class="bg edit_wrap">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
			<el-col v-if="user_group === '管理员' || $check_field('get','pet_name') || $check_field('add','pet_name') || $check_field('set','pet_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宠物名称" prop="pet_name">
					<el-input id="pet_name" v-model="form['pet_name']" placeholder="请输入宠物名称"
							  v-if="user_group === '管理员' || (form['pet_foster_care_id'] && $check_field('set','pet_name')) || (!form['pet_foster_care_id'] && $check_field('add','pet_name'))" :disabled="disabledObj['pet_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','pet_name')">{{form['pet_name']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','pet_type') || $check_field('add','pet_type') || $check_field('set','pet_type')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宠物类型" prop="pet_type">
					<el-select id="pet_type" v-model="form['pet_type']"
						v-if="user_group === '管理员' || (form['pet_foster_care_id'] && $check_field('set','pet_type')) || (!form['pet_foster_care_id'] && $check_field('add','pet_type'))">
						<el-option v-for="o in list_pet_type" :key="o['pet_type']" :label="o['pet_type']"
							:value="o['pet_type']">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','pet_type')">{{form['pet_type']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','pet_sex') || $check_field('add','pet_sex') || $check_field('set','pet_sex')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宠物性别" prop="pet_sex">
					<el-input id="pet_sex" v-model="form['pet_sex']" placeholder="请输入宠物性别"
							  v-if="user_group === '管理员' || (form['pet_foster_care_id'] && $check_field('set','pet_sex')) || (!form['pet_foster_care_id'] && $check_field('add','pet_sex'))" :disabled="disabledObj['pet_sex_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','pet_sex')">{{form['pet_sex']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','pet_age') || $check_field('add','pet_age') || $check_field('set','pet_age')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宠物年龄" prop="pet_age">
					<el-input id="pet_age" v-model="form['pet_age']" placeholder="请输入宠物年龄"
							  v-if="user_group === '管理员' || (form['pet_foster_care_id'] && $check_field('set','pet_age')) || (!form['pet_foster_care_id'] && $check_field('add','pet_age'))" :disabled="disabledObj['pet_age_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','pet_age')">{{form['pet_age']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','foster_care_status') || $check_field('add','foster_care_status') || $check_field('set','foster_care_status')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="寄养状态" prop="foster_care_status">
					<el-select id="foster_care_status" v-model="form['foster_care_status']"
						v-if="user_group === '管理员' || (form['pet_foster_care_id'] && $check_field('set','foster_care_status')) || (!form['pet_foster_care_id'] && $check_field('add','foster_care_status'))">
						<el-option v-for="o in list_foster_care_status" :key="o" :label="o" :value="o">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','foster_care_status')">{{form['foster_care_status']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','foster_care_pictures') || $check_field('add','foster_care_pictures') || $check_field('set','foster_care_pictures')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="寄养图片" prop="foster_care_pictures">
					<el-upload :disabled="disabledObj['foster_care_pictures_isDisabled']" id="foster_care_pictures" class="avatar-uploader" drag
						accept="image/gif, image/jpeg, image/png, image/jpg" action="" :http-request="upload_foster_care_pictures"
						:show-file-list="false" v-if="user_group === '管理员' || (form['pet_foster_care_id'] && $check_field('set','foster_care_pictures')) || (!form['pet_foster_care_id'] && $check_field('add','foster_care_pictures'))">
						<img v-if="form['foster_care_pictures']" :src="$fullUrl(form['foster_care_pictures'])" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
					<el-image v-else-if="$check_field('get','foster_care_pictures')" style="width: 100px; height: 100px"
						:src="$fullUrl(form['foster_care_pictures'])" :preview-src-list="[$fullUrl(form['foster_care_pictures'])]">
						<div slot="error" class="image-slot">
							<img src="../../../public/img/error.png" style="width: 90px; height: 90px" />
						</div>
					</el-image>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','user_name') || $check_field('add','user_name') || $check_field('set','user_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="用户姓名" prop="user_name">
						<div v-if="user_group !== '管理员'">
							{{ get_user_session_user_name(form['user_name']) }}
							<!--<el-input id="business_name" v-model="form['user_name']" placeholder="请输入用户姓名"-->
							<!--v-if="user_group === '管理员' || (form['pet_foster_care_id'] && $check_field('set','user_name')) || (!form['pet_foster_care_id'] && $check_field('add','user_name'))" :disabled="disabledObj['user_name_isDisabled']"></el-input>-->
							<!--<div v-else-if="$check_field('get','user_name')">{{form['user_name']}}</div>-->
							<el-select v-if="user_group === '管理员' || (form['pet_foster_care_id'] && $check_field('set','user_name')) || (!form['pet_foster_care_id'] && $check_field('add','user_name'))" id="user_name" v-model="form['user_name']" :disabled="disabledObj['user_name_isDisabled']">
								<el-option v-for="o in list_user_user_name" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','user_name')" id="user_name" v-model="form['user_name']" :disabled="true">
								<el-option v-for="o in list_user_user_name" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="user_name" v-model="form['user_name']" :disabled="disabledObj['user_name_isDisabled']">
							<el-option v-for="o in list_user_user_name" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','foster_care_time') || $check_field('add','foster_care_time') || $check_field('set','foster_care_time')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="寄养时间" prop="foster_care_time">
					<el-date-picker :disabled="disabledObj['foster_care_time_isDisabled']" v-if="user_group === '管理员' || (form['pet_foster_care_id'] && $check_field('set','foster_care_time')) || (!form['pet_foster_care_id'] && $check_field('add','foster_care_time'))" id="foster_care_time"
						v-model="form['foster_care_time']" type="datetime" placeholder="选择日期时间">
					</el-date-picker>
					<div v-else-if="$check_field('get','foster_care_time')">{{form['foster_care_time']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','foster_care_remarks') || $check_field('add','foster_care_remarks') || $check_field('set','foster_care_remarks')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="寄养备注" prop="foster_care_remarks">
					<el-input type="textarea" id="foster_care_remarks" v-model="form['foster_care_remarks']" placeholder="请输入寄养备注"
						v-if="user_group === '管理员' || (form['pet_foster_care_id'] && $check_field('set','foster_care_remarks')) || (!form['pet_foster_care_id'] && $check_field('add','foster_care_remarks'))" :disabled="disabledObj['foster_care_remarks_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','foster_care_remarks')">{{form['foster_care_remarks']}}</div>
				</el-form-item>
			</el-col>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item>
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				field: "pet_foster_care_id",
				url_add: "~/api/pet_foster_care/add?",
				url_set: "~/api/pet_foster_care/set?",
				url_get_obj: "~/api/pet_foster_care/get_obj?",
				url_upload: "~/api/pet_foster_care/upload?",

				query: {
					"pet_foster_care_id": 0,
				},

				form: {
					"pet_name":'', // 宠物名称
					"pet_type":'', // 宠物类型
					"pet_sex":'', // 宠物性别
					"pet_age":'', // 宠物年龄
					"foster_care_status":'', // 寄养状态
					"foster_care_pictures":'', // 寄养图片
					"user_name": 0, // 用户姓名
					"foster_care_time":'', // 寄养时间
					"foster_care_remarks":'', // 寄养备注
					"pet_foster_care_id": 0, // ID

				},
				disabledObj:{
					"pet_name_isDisabled": false,
					"pet_type_isDisabled": false,
					"pet_sex_isDisabled": false,
					"pet_age_isDisabled": false,
					"foster_care_status_isDisabled": false,
					"foster_care_pictures_isDisabled": false,
					"user_name_isDisabled": false,
					"foster_care_time_isDisabled": false,
					"foster_care_remarks_isDisabled": false,
				},
				//宠物类型选项列表
				list_pet_type: [],
				//寄养状态选项列表
				list_foster_care_status: ['待寄养','已寄养'],
				// 用户列表
				list_user_user_name: [],
				// 用户组
				group_user_user_name: "",
			}
		},
		methods: {
			/**
			 * 获取宠物类型列表
			 */
			async get_list_pet_type() {
				var json = await this.$get("~/api/pet_classification/get_list?");
				if(json.result && json.result.list){
					this.list_pet_type = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			/**
			 * 上传寄养图片
			 * @param {Object} param图片参数
			 */
			upload_foster_care_pictures(param){
				this.uploadFile(param.file, "foster_care_pictures");
			},
			/**
			 * 获取普通用户用户列表
			 */
			async get_list_user_user_name() {
                // if(this.user_group !== "管理员" && this.form["user_name"] === 0) {
                //     this.form["user_name"] = this.user.user_id;
                // }
                var json = await this.$get("~/api/user/get_list?user_group=普通用户");
                if(json.result && json.result.list){
                    this.list_user_user_name = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
			/**
			 * 获取普通用户用户组
			 */
			async get_group_user_user_name() {
				this.form["user_name"] = this.user.user_id;
				var json = await this.$get("~/api/user_group/get_obj?name=普通用户");
				if(json.result && json.result.obj){
					this.group_user_user_name = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_user_name(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_user_name.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
						for (let key in _this.form) {
							arrForm.push(key)
						}
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
                for (var j = 0; j < arrForm.length; j++) {
                  if (arr[i] === arrForm[j]) {
                    if (arr[i] !== "user_name") {
                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                      break;
                    } else {
                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                    }
                  }
                }
              }
						}
					}
				});
			},
			get_user_user_name(id){
				var obj = this.list_user_user_name.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},

			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
				/**
				* 请求列表前
				* @param {Object} param
				*/
				var user_group = this.user.user_group;
				if (user_group !== "管理员") {
					switch (user_group) {
						case "用户姓名":
							if(param["user_name"] > 0){
								param["user_name"] = this.user.user_id;
							}
							break;
					}
				}
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
							}
						})
					})
				}
				this.form["foster_care_time"] = this.$toTime(parseInt(this.form["foster_care_time"]),"yyyy-MM-dd hh:mm:ss")
				$.db.del("form");
				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){
        if(this.form["foster_care_time"]=="0000-00-00 00:00:00"){
          this.form["foster_care_time"] = null;
        }
				if(parseInt(this.form["foster_care_time"]) > 9999){
					this.form["foster_care_time"] = this.$toTime(parseInt(this.form["foster_care_time"]),"yyyy-MM-dd hh:mm:ss")
				}
			},

			is_view(){
				var bl = this.user_group == "管理员";

				if(!bl){
					bl = this.$check_action('/pet_foster_care/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/pet_foster_care/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/pet_foster_care/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/pet_foster_care/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/pet_foster_care/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
			this.get_list_pet_type();
			this.get_list_user_user_name();
			this.get_group_user_user_name();
		},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
